package com.example.final_project.Home.frag;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Home.adapter.HomeAdapter;
import com.example.final_project.Home.entry.CarNews;
import com.example.final_project.R;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private Context context;
    private List<CarNews> cars;
    private View view;
    private int pageNow=1;
    private int pageSize = 4;
    private RecyclerView new_rv;
    public static final int INIT=0;
    public static final int LOADING=1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_home, container, false);

        context = this.getContext();

        getNews();
        //绑定自定义Adapter
        new_rv = view.findViewById(R.id.new_rv);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 1);
        new_rv.setLayoutManager(gridLayoutManager);
        RefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(context));
        refreshLayout.setRefreshFooter(new ClassicsFooter(context));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if(pageNow < 3){
                    pageNow++;
                }else{
                    pageNow=1;
                }

                LoadNews();
                refreshlayout.finishLoadMore(true);//传入false表示加载失败
            }
        });

        return view;


    }
    HomeAdapter homeAdapter;
    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case INIT:
                    cars = (List<CarNews>)msg.obj;
                    homeAdapter = new HomeAdapter(context,cars);
                    new_rv.setAdapter(homeAdapter);
                    break;
                case LOADING:
                    List<CarNews> list = (List<CarNews>)msg.obj;
                    homeAdapter.setData(list);
                    break;
            }
        }
    };

    //获取汽车新闻信息
    public void getNews(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/findCarByPage")
                .bind(context)
                .addUrlPara("pageNow",pageNow)
                .addUrlPara("pageSize",pageSize)
                .setOnResList(CarNews.class,news ->{
                    Message message = Message.obtain();
                    message.what=INIT;
                   message.obj = news;
                   handler.sendMessage(message);
                })
                .get();
    }

    public void LoadNews(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/findCarByPage")
                .bind(context)
                .addUrlPara("pageNow",pageNow)
                .addUrlPara("pageSize",pageSize)
                .setOnResList(CarNews.class,news ->{
                    Message message = Message.obtain();
                    message.what=LOADING;
                    message.obj=news;
                    handler.sendMessage(message);
                })
                .get();
    }

}